﻿// <copyright file="WaveWindow.xaml.cs" company="Waveform Lab.NET">
// Signal processing by PC oscilloscope
// http://waveformlab.codeplex.com/
//
// Copyright (c) 2011 Waveform Lab.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;

using MemoryModule;
using AudioModule;
using F2fModule;
using FFTModule;


namespace WaveformLab
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class WaveWindow : Window
    {
        private enum SignalProcessing { None, FFT, F2F };
        private SignalProcessing Proc = SignalProcessing.None;
        private AudioModueView Dev;
        private FFTModuleView fftobj;
        private F2fModuleView f2fobj;
        private util dir;

        public WaveWindow()
        {
            InitializeComponent();
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
            dir = new util();
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            WaveWindowViewModel vm = (WaveWindowViewModel)this.DataContext;
            Dev = (AudioModueView)vm.ModuleDeviceContainer.Content;
            if (Dev == null) return;

            string appname = vm.ModuleApplicationName;
            if (appname == null) appname = string.Empty;

            if (Dev.Captured)
            {
                if ((Dev.EnableLeft == false) && (Dev.EnableRight == false)) return;
                string name = dir.BaseDir + "sampling" + Dev.BufferNo.ToString() + ".csv";
                dir.Name = name;
                string name_l = dir.BaseDir + "sample_l" + Dev.BufferNo.ToString() + ".csv";
                string name_r = dir.BaseDir + "sample_r" + Dev.BufferNo.ToString() + ".csv";
                try { var r = new StreamReader(name); }
                catch { return; }
                try { var r = new StreamReader(name_l); }
                catch { return; }
                try { var r = new StreamReader(name_r); }
                catch { return; }

                switch (appname)
                {
                    case "FFTModule":
                        fftobj = (FFTModuleView)vm.ModuleApplicationContainer.Content;
                        FFTProc(name_l, name_r);
                        Graph.LoadCsv(fftobj.FileName, fftobj.NameX, fftobj.NameY);
                        Graph.ShowAxis();
                        break;
                    case "F2fModule":
                        f2fobj = (F2fModuleView)vm.ModuleApplicationContainer.Content;
                        Graph.LoadCsv(name, Dev.NameX, Dev.NameY);
                        Graph.ShowAxis();
                        F2FProc(Dev.seq_start_l, Dev.seq_start_r,name_l, name_r);
                        break;
                    default:
                        Graph.LoadCsv(name, Dev.NameX, Dev.NameY);
                        Graph.ShowAxis();
                        break;
                }

                Dev.Captured = false;
            }
 
        }

        private void FFTProc(string l, string r)
        {
            fftobj.NyquistRate = Dev.SampleRate/2;
            fftobj.ActualSamplingNumber = Dev.SampleNumber;
            string msg = fftobj.Processing(l,r,Dev.EnableLeft,Dev.EnableRight);
            textBoxConsole.Text = msg;
        }

        private void F2FProc(int sl, int sr,string l, string r)
        {
            string msg = f2fobj.Processing(sl, sr, l, r, Dev.EnableLeft,Dev.EnableRight);
            textBoxConsole.Text = msg;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem obj = (MenuItem)sender;
            switch (obj.Name)
            {
                case "Open":
                    Graph.OpenCsv("","");
                    break;
                case "Save":
                    var dlg = new OpenFileDialog();
                    dlg.Filter = ".csv files|*.csv";
                    dlg.DefaultExt = ".csv";
                    dlg.CheckFileExists = false;
                    if (dlg.ShowDialog().Value)
                    {
                        try
                        {
                            File.Copy(dir.Name, dlg.FileName);
                        }
                        catch { MessageBox.Show("No effective data"); }
                    }           
                    break;
                case "FFT":
                    break;
                case "F2F":
                    break;
                case "Exit":
                    Application.Current.Shutdown();
                    break;
                case "Help":
                    System.Diagnostics.Process.Start("http://www.kiko-s.com/");
                    break;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
